package net.jarang.support.db;

import java.sql.Types;
import java.util.Properties;

import net.jarang.support.PropertiesUtil;
import net.jarang.support.StringUtil;


public class FieldMapper {
	private static final String RESOURCE_NAME = "dictionary.properties";
	private Column column;

	public FieldMapper(Column column) {
		this.column = column;
	}
	
	public Column getColumn() {
		return column;
	}
	
	public boolean isDateType() {
		if (Types.DATE == column.getDataType() || 
				Types.TIME == column.getDataType() || 
				Types.TIMESTAMP == column.getDataType()) 
			return true;
		return false;
	}	
	
	public String getFieldName() {
		String formalColumnName = getFormalColumnName(column.getColumnName());
		return StringUtil.camelize(formalColumnName, false);
	}

	public String getFieldUpperName() {
		String formalColumnName = getFormalColumnName(column.getColumnName());
		return StringUtil.camelize(formalColumnName);
	}
	
	private String getFormalColumnName(String columnName) {
		Properties properties = PropertiesUtil.getProperties(RESOURCE_NAME);
		String formalColumnName = properties.getProperty(columnName);
		return formalColumnName == null? columnName : formalColumnName;
	}	
	
	public String getFieldType() {
		String fieldType = null;
		
		if (Types.CHAR == column.getDataType()) {
			fieldType = "String";
		} else if (Types.VARCHAR == column.getDataType()) {
			fieldType = "String";
		} else if (Types.LONGVARCHAR == column.getDataType()) {
			fieldType = "String";
		} else if (Types.BLOB == column.getDataType()) {
			fieldType = "String";			
		} else if (Types.CLOB == column.getDataType()) {
			fieldType = "String";			
		} else if (Types.TINYINT == column.getDataType()) {
			fieldType = "int";
		} else if (Types.SMALLINT == column.getDataType()) {
			fieldType = "int";			
		} else if (Types.INTEGER == column.getDataType()) {
			fieldType = "int";			
		} else if (Types.BIGINT == column.getDataType()) {
			fieldType = "int";			
		} else if (Types.DATE == column.getDataType()) {
			fieldType = "Date";
		} else if (Types.TIME == column.getDataType()) {
			fieldType = "Date";
		} else if (Types.TIMESTAMP == column.getDataType()) {
			fieldType = "Date";
		} else {
			fieldType = "String";
		}

		return fieldType;
	}
}
